home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UCRASM25.ARJ / STRDUP.ASM < prev    next >
Assembly Source File  |  1991-10-12  |  1KB  |  74 lines

  1. StdGrp        group    stdlib,stddata
  2. stddata        segment    para public 'sldata'
  3. stddata        ends
  4. ;
  5. stdlib        segment    para public 'slcode'
  6.         assume    cs:stdgrp
  7. ;
  8.         extrn    sl_malloc:far
  9. ;
  10. ; strdup- On entry, es:di points at a source string.  Strdup allocates
  11. ;      storage for a new string the same size and copies the data from
  12. ;      the source string to the new destination string.  Returns a ptr
  13. ;      to the new string in ES:dI.  Calls malloc to allocate storage
  14. ;      for the new string.
  15. ;
  16. ; inputs:
  17. ;        es:di-  Address of string to copy.
  18. ;
  19. ; outputs:
  20. ;        es:di-  Ptr to newly allocated string.
  21. ;
  22. ;
  23.         public    sl_strdup
  24. ;
  25. sl_strdup    proc    far
  26.         push    ds
  27.         push    cx
  28.         push    ax
  29.         pushf
  30.         push    si
  31. ;
  32.         mov    ax, es
  33.         mov    ds, ax
  34.         cld
  35.         mov    al, 0
  36.         mov    cx, 0ffffh
  37.         mov    si, di
  38.     repne    scasb
  39.         neg    cx
  40.         dec    cx
  41.         push    cx
  42.         call    sl_malloc
  43.         pop    cx
  44.         jc    QuitStrDup
  45.         push    di
  46.         shr    cx, 1
  47.         jnc    IsWord
  48.         lodsb
  49.         stosb
  50. IsWord:    rep    movsw
  51. ;
  52.         pop    di
  53.         pop    si
  54.         popf
  55.         pop    ax
  56.         pop    cx
  57.         pop    ds
  58.         clc
  59.         ret
  60. ;
  61. QuitStrDup:    pop    si
  62.         popf
  63.         pop    ax
  64.         pop    cx
  65.         pop    ds
  66.         stc
  67.         ret
  68. ;
  69. sl_strdup    endp
  70. ;
  71. ;
  72. stdlib        ends
  73.         end
  74.